package jpave.dataholders;

import java.sql.Connection;
import java.util.Vector;
import java.io.Serializable;

import util.Debug.ErrorReport;
import util.methods.Version;
import jpave.database.JPaveDBWrapper;

/**
 * Holds all of the information needed to open an
 * entire assembly of contigs (i.e. database URL and assembly ID).
 * 
 * We use to allow multiple assemblies in a database, hence, the separtion of AssemblyData and DatabaseData
*/
public class AssemblyData extends Object implements Serializable, Comparable<AssemblyData>
{
	private static final long serialVersionUID = -3512972231826523680L;
	public void setDescription ( String str ) { strDescription = str; }
	public String getDescription () { return strDescription; }

	public void setDBData ( DatabaseData inDB ) { db = inDB; }
	public DatabaseData getDBData () { return db; };
	public String getDBName() {return db.getDBName();}
	
	public int compareTo(AssemblyData rightAssembly)
	{			
		int n = strAssemblyID.compareTo( rightAssembly.strAssemblyID );	
		
		if ( n == 0 )
			n = db.compareTo( rightAssembly.db );
		return n;
	}
	
	static public int compareDB ( AssemblyData assembly1, AssemblyData assembly2 )
	{
		return assembly1.db.compareTo( assembly2.db );
	}
    
    public Connection createConnection ( ) throws Exception
    {
        return db.createDBConnection();
    }
 
    public JPaveDBWrapper createDBConnection ( ) throws Exception
    {   	
        return new JPaveDBWrapper ( db.createDBConnection(), strAssemblyID );
    }
	
	public void setAssemblyID ( String str ) { 
		if (str != null) 
			strAssemblyID = str;
	}
	public String getAssemblyID () { return strAssemblyID; }

	public void setAssemblyDate ( String str ) { 
		if (str != null)
			strAssemblyDate = str;
	}	

	public String getAssemblyDate () { return strAssemblyDate; }
	
	public void setAnnotationDate ( String str ) { 
		if (str != null)
			strAnnotationDate = str;
	}	
	
	public String getAnnotationDate () { return strAnnotationDate; }

	public void setAID( int i ) {AID = i;} 
	public int getAID () { return AID; }
	
	public void setAssemblyUser( String str ) { 
		if (str != null)
			strAssemblyUser = str;
	}		
	public String getAssemblyUser () { return strAssemblyUser; } 
	
	public void setAssemblyPath( String str ) { 
		if (str != null)
			strAssemblyPath = str;
	}		
	public String getAssemblyPath () { return strAssemblyPath; } 

	public void setCAPparams( String str ) { 
		if (str != null)
			strCAPparams = str;
	}		
	public String getCAPparams () { return strCAPparams; } 
	
	public void setDBversion( double d) {
		strDBversion = Double.toString(d);
	}
	public void setDBversion( String s) {
		strDBversion = s;
	}
	// Version.getDBversion returns double
	public String getDBversion() {return strDBversion;}
	
	public String toString ( ) { return getDescription (); }
    
	/**
	 * called from sessionData
	 */
	public boolean overview ( Vector<String> lines ) throws Exception
	{
		// created in OveriewDBWrapper
		return createDBConnection().overview (AID, lines );
	}
	    
    /**
     * called from SessionData.createPAVElistFromJpaveConfig
     */
    static public Vector<AssemblyData> getAssmInfoForEachAssm ( Vector<DatabaseData> dbs )
	{
	    	Vector<AssemblyData> list = new Vector<AssemblyData> ();
	
	    	for (DatabaseData db : dbs)
	    	{
	    		for (String id : db.getAssemblyIDs()) { 
	    			AssemblyData newAssembly = new AssemblyData ();
	    	        newAssembly.setDescription( id );
	    	        newAssembly.setAssemblyID( id );  
	    	        newAssembly.setDBData( db );
	    	        try {
		    	        Connection conn = db.createDBConnection();
		    	        double v = Version.fltSchema(conn);
		   
		    	        newAssembly.setDBversion(Version.sDBver);
		    	        if (v > 2.0) { // User will be asked to update database
		    	        		JPaveDBWrapper.getSetAssemblyParams( conn, newAssembly);	
		    	        }
		    	        conn.close();
	    	        }
	    	        catch (Exception e) { 
		    	        	ErrorReport.reportError(e, "Error: opening database connection for " + id);
	    	        }	    	        
	    	        list.add( newAssembly );
	    		}
	    	}
		return list;
	}

    /*****************************************************************/
	private int AID;
	private String strDescription = "";
	private String strAssemblyID = "";
	private String strAssemblyDate = ""; 
	private String strAnnotationDate = ""; 	
	private String strAssemblyUser = ""; 	
	private String strAssemblyPath = "";
	private String strCAPparams = "";
	private String strDBversion = "";
    private DatabaseData db;      
}
